package com.ruoyi.pingan.fymodule.busManage.busSubAcct.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.pingan.annotation.CommonLog;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.entity.busSubAcct;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.param.busSubAcctAddParam;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.param.busSubAcctEditParam;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.param.busSubAcctIdParam;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.param.busSubAcctPageParam;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.service.BusSubAcctService;
import com.ruoyi.pingan.fymodule.thirdpa.util.CommonResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 商户子账号控制器
 *
 * @author zjf
 * @date  2025/07/10 14:01
 */
@Tag(name = "商户子账号控制器")
@RestController
@Validated
public class busSubAcctController {

    @Resource
    private BusSubAcctService busSubAcctService;

    /**
     * 获取商户子账号分页
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "获取商户子账号分页")
    @SaCheckPermission("/biz/busSubAcct/page")
    @GetMapping("/biz/busSubAcct/page")
    public CommonResult<Page<busSubAcct>> page(busSubAcctPageParam busSubAcctPageParam) {
        return CommonResult.data(busSubAcctService.page(busSubAcctPageParam));
    }

    /**
     * 添加商户子账号
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "添加商户子账号")
    @CommonLog("添加商户子账号")
    @SaCheckPermission("/biz/busSubAcct/add")
    @PostMapping("/biz/busSubAcct/add")
    public CommonResult<String> add(@RequestBody @Valid busSubAcctAddParam busSubAcctAddParam) {
        busSubAcctService.add(busSubAcctAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑商户子账号
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "编辑商户子账号")
    @CommonLog("编辑商户子账号")
    @SaCheckPermission("/biz/busSubAcct/edit")
    @PostMapping("/biz/busSubAcct/edit")
    public CommonResult<String> edit(@RequestBody @Valid busSubAcctEditParam busSubAcctEditParam) {
        busSubAcctService.edit(busSubAcctEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除商户子账号
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "删除商户子账号")
    @CommonLog("删除商户子账号")
    @SaCheckPermission("/biz/busSubAcct/delete")
    @PostMapping("/biz/busSubAcct/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                               List<busSubAcctIdParam> busSubAcctIdParamList) {
        busSubAcctService.delete(busSubAcctIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取商户子账号详情
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "获取商户子账号详情")
    @SaCheckPermission("/biz/busSubAcct/detail")
    @GetMapping("/biz/busSubAcct/detail")
    public CommonResult<busSubAcct> detail(@Valid busSubAcctIdParam busSubAcctIdParam) {
        return CommonResult.data(busSubAcctService.detail(busSubAcctIdParam));
    }



    /**
     * 下载商户子账号导入模板
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "下载商户子账号导入模板")
    @GetMapping(value = "/biz/busSubAcct/downloadImportTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportTemplate(HttpServletResponse response) throws IOException {
        busSubAcctService.downloadImportTemplate(response);
    }

    /**
     * 导入商户子账号
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "导入商户子账号")
    @CommonLog("导入商户子账号")
    @SaCheckPermission("/biz/busSubAcct/importData")
    @PostMapping("/biz/busSubAcct/importData")
    public CommonResult<JSONObject> importData(@RequestPart("file") MultipartFile file) {
        return CommonResult.data(busSubAcctService.importData(file));
    }

    /**
     * 导出商户子账号
     *
     * @author zjf
     * @date  2025/07/10 14:01
     */
    @Operation(summary = "导出商户子账号")
    @SaCheckPermission("/biz/busSubAcct/exportData")
    @PostMapping(value = "/biz/busSubAcct/exportData", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportData(@RequestBody List<busSubAcctIdParam> busSubAcctIdParamList, HttpServletResponse response) throws IOException {
        busSubAcctService.exportData(busSubAcctIdParamList, response);
    }
}
